package
{
	import com.game.element.BaseElement;
	import com.game.res.UIRescource;
	import com.interfaces.game.GlobalAPI;
	import com.interfaces.general.AbstractDisplayObject;
	import com.interfaces.general.element.AbstractElement;
	import com.interfaces.general.element.RoleElement;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.drm.AddToDeviceGroupSetting;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	import map.Char;
	import map.MapTileEvent;
	import map.SceneMap;
	
	import net.hires.debug.Stats;
	
	[SWF(width="1000", height="600", frameRate="40", backgroundColor="#000000")]
	public class MapTest extends AbstractDisplayObject {
		
		private var bit:Bitmap = new Bitmap;
		private var bitt:Bitmap = new (UIRescource.DI_TU) as Bitmap;
		
		private var sceneMap:SceneMap = new SceneMap;
		
		private var dic:Dictionary = new Dictionary;
		
		private var sele:Shape = new Shape;
		
		private var role:RoleElement = new RoleElement;
		public function MapTest() {
			
			super();
			var stats:Stats = new Stats;
			stats.y  =200;
			addChild(stats);
			
			sceneMap.width = 1000;
			sceneMap.height = 600;
			sceneMap.drawGeZi();
			sceneMap.x = 0;
			sceneMap.y = 0;
			addChild(sceneMap);
			
			sele.graphics.clear();
			sele.graphics.beginFill(0xFF9900,1);
			sele.graphics.drawRect(0,0,SceneMap.CELL_WIDTH,SceneMap.CELL_HEIGHT);
			sele.graphics.endFill();
			
			addEventListener(MouseEvent.CLICK,clickHandler);
			
			initData(bitt.bitmapData,dic);
			sceneMap.addEventListener(MapTileEvent.REQUIRE_MAP_DATA,requireData);
			
			addEventListener(Event.ADDED_TO_STAGE,addToStageHandler);
		}
		
		private function addToStageHandler(e:Event):void{
			GlobalAPI.init(this.stage);
			
			role.x = 100;
			role.y = 100;
			role.setGap(1000/12);
			addChild(role);
		}
		
		private function enterFrameHandler(e:Event):void{
			sceneMap.updateShowRect(role.x,role.y);
		}
		
		private function clickHandler(e:Event):void{
			trace("updateShowRect",mouseX,mouseY);
//			var p:Point = sceneMap.localToMap(new Point(mouseX,mouseY));
//			sceneMap.updateShowRect(mouseX-100,mouseY-100);
		
			
			var roleX:int =int(mouseX/SceneMap.CELL_WIDTH)*SceneMap.CELL_WIDTH+SceneMap.CELL_WIDTH/2
			var roleY:int = int(mouseY/SceneMap.CELL_HEIGHT)*SceneMap.CELL_HEIGHT+SceneMap.CELL_HEIGHT/2
		
			role.walkStart([[roleX,roleY]]);
			sceneMap.updateShowRect(role.x,role.y);
		}
		
		private static function initData(sour:BitmapData,dic:Dictionary):void{
			sour.lock();
			for (var tilex:int=0;tilex<sour.width/int(SceneMap.CELL_WIDTH);tilex++){
				for (var tiley:int=0;tiley<sour.height/int(SceneMap.CELL_HEIGHT);tiley++){
					var bit:BitmapData = new BitmapData(SceneMap.CELL_WIDTH,SceneMap.CELL_HEIGHT);
					bit.copyPixels(
						sour,
						new Rectangle(
							tilex*SceneMap.CELL_WIDTH,
							tiley*SceneMap.CELL_HEIGHT,
							SceneMap.CELL_HEIGHT,
							SceneMap.CELL_HEIGHT),new Point);
					dic[""+tilex+"_"+tiley+""] = bit;
//					trace("save :"+""+tilex+"_"+tiley+"");
				}
			}
			sour.unlock();
		}
		
		private function requireData(e:MapTileEvent):void{
			var key:String =int(e.tilex/SceneMap.CELL_WIDTH) +"_"+int(e.tiley/SceneMap.CELL_HEIGHT) ;
			if (dic[key] == null){
//				trace("can not find"+key);
				return;
			}
//			trace("find"+key);
			e.bitmapData = dic[key] as BitmapData;
			sceneMap.setMapCell(e);
		}
	}
}